package com.szh.taobaounion.Presenter.impl;

import com.szh.taobaounion.Presenter.IOnSellPagePresenter;
import com.szh.taobaounion.model.Api;
import com.szh.taobaounion.model.domain.OnSellContent;
import com.szh.taobaounion.utils.LogUtils;
import com.szh.taobaounion.utils.RetrofitManager;
import com.szh.taobaounion.utils.UrlUtils;
import com.szh.taobaounion.view.IOnSellPageCallaBack;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ISellPagePresenterImpl implements IOnSellPagePresenter {
    private static final int DEFAULT_PAGE = 1;
    private int mCurrentPage = DEFAULT_PAGE;
    private IOnSellPageCallaBack mOnSellPageCallBack = null;
    private final Api mApi;

    public ISellPagePresenterImpl() {
        Retrofit retrofit = RetrofitManager.getInstance().getmRetrofit();
        mApi = retrofit.create(Api.class);
    }

    @Override
    public void getOnSellContent() {
        if (mIsLoading) {
            return;
        }
        mIsLoading=true;
        //通知ui状态为加载中
        if (mOnSellPageCallBack != null) {
            mOnSellPageCallBack.onLoading();
        }
        //获取特惠内容

        String targetUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);
        Call<OnSellContent> task = mApi.getOnSellPageContent(targetUrl);
        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(Call<OnSellContent> call, Response<OnSellContent> response) {
                mIsLoading=false;
                int resultCode = response.code();
                LogUtils.d(ISellPagePresenterImpl.this, "sell result code --->" + resultCode);
                if (resultCode == HttpURLConnection.HTTP_OK) {
                    OnSellContent result = response.body();
                    OnSuccess(result);

                } else {
                    OnError();
                }
            }

            @Override
            public void onFailure(Call<OnSellContent> call, Throwable t) {
                OnError();
            }
        });

    }

    private void OnSuccess(OnSellContent result) {
        if (mOnSellPageCallBack != null) {
            try {
                if (isEmpty(result)){
                    onEmpty();
                }else {
                    mOnSellPageCallBack.onContentLoadedSuccess(result);
                }
            }catch (Exception e){
                e.printStackTrace();
                onEmpty();
            }

        }
    }
    private boolean isEmpty(OnSellContent content) {
        int size = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();
        return size == 0;
    }
    private void onEmpty(){
        if (mOnSellPageCallBack != null) {
            mOnSellPageCallBack.onEmpty();
        }
    }

    private void OnError() {
        mIsLoading=false;
        if (mOnSellPageCallBack != null) {
            mOnSellPageCallBack.onError();
        }
    }

    @Override
    public void reLoad() {
     //重新加载
        this.getOnSellContent();
    }
    /**
     * 当前加载状态
     */
    private boolean mIsLoading = false;
    @Override
    public void LoaderMore() {
     //加载更多
        if (mIsLoading) {
            return;
        }
        mIsLoading=true;
        mCurrentPage++;
        String targetUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);
        Call<OnSellContent> task = mApi.getOnSellPageContent(targetUrl);
        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(Call<OnSellContent> call, Response<OnSellContent> response) {
                mIsLoading=false;
                int resultCode = response.code();
                LogUtils.d(ISellPagePresenterImpl.this, "sell result code --->" + resultCode);
                if (resultCode == HttpURLConnection.HTTP_OK) {
                    OnSellContent result = response.body();
                    if (mCurrentPage <= 4) {
                        onMoreLoaded(result);
                    }else {
                        mOnSellPageCallBack.onMoreLoadedEmpty();
                    }

                } else {
                    onMoreLoadedError();
                }
            }

            @Override
            public void onFailure(Call<OnSellContent> call, Throwable t) {
                onMoreLoadedError();
            }
        });

    }

    private void onMoreLoadedError() {
        mIsLoading = false;
        mCurrentPage--;
        mOnSellPageCallBack.onMoreLoadedError();
    }

    /**
     * 加载更多的结果，通知UI更新.
     *
     * @param result
     */
    private void onMoreLoaded(OnSellContent result) {
        if(mOnSellPageCallBack != null) {
            if(isEmpty(result)) {
                mCurrentPage--;
                mOnSellPageCallBack.onMoreLoadedEmpty();
            } else {
                mOnSellPageCallBack.onMoreLoaded(result);
            }
        }
    }

    @Override
    public void registerViewCallBack(IOnSellPageCallaBack callBack) {
        this.mOnSellPageCallBack = callBack;

    }

    @Override
    public void unregisterViewCallBack(IOnSellPageCallaBack callBack) {
        this.mOnSellPageCallBack = null;
    }
}
